#include "pagegenerator.h"
#include "haha_global.h"

namespace Haha
{
class PageGeneratorPrivate
{
public:
    int index = 1;
    PageGenerator::Category category = PageGenerator::New;
};

PageGenerator::PageGenerator(QObject *parent) : QObject(parent)
{
    d_ptr = new PageGeneratorPrivate;
}

PageGenerator::~PageGenerator()
{
    delete d_ptr;
}

void PageGenerator::setCategory(PageGenerator::Category cat)
{
    if (d_ptr->category != cat) {
        d_ptr->category = cat;
        d_ptr->index = 1;
    }
}

PageGenerator::Category PageGenerator::category() const
{
    return d_ptr->category;
}

PageGenerator* PageGenerator::instance()
{
    static PageGenerator _inst;
    return &_inst;
}

QUrl PageGenerator::previous()
{
    int pageIndex = --d_ptr->index;
    if (pageIndex < 1)
        pageIndex = 1;

    QString strUrl;
    if (d_ptr->category == New)
        strUrl = QString("%1/%2").arg(Haha::Constant::HAHANEWPAGE).arg(pageIndex);
    else
        strUrl = QString("%1/%2").arg(Haha::Constant::HAHAGOODPAGE).arg(pageIndex);
    return QUrl(strUrl);
}

QUrl PageGenerator::next()
{
    int pageIndex = ++d_ptr->index;

    QString strUrl;
    if (d_ptr->category == New)
        strUrl = QString("%1/%2").arg(Haha::Constant::HAHANEWPAGE).arg(pageIndex);
    else
        strUrl = QString("%1/%2").arg(Haha::Constant::HAHAGOODPAGE).arg(pageIndex);
    return strUrl;
}

} // namespace haha
